/*
 * GraphIF.java
 *
 * Created on Feb 23, 2009, 7:03:47 PM
 */
package view;

import controller.Controller;
import java.util.ArrayList;
import jgf.Attribute;
import jgf.beans.ArcBean;
import jgf.beans.GraphBean;
import jgf.beans.NodeBean;
import model.observer.JGSObserver;
import view.panels.JGSPanel;
import view.panels.graphical.GraphicalPanel;

/**
 * 
 * @author Marcos Garcia Ortega <a href="mailto:thiamath@gmail.com">&lt;thiamath@gmail.com&gt;</a>
 */
public class GraphIF extends javax.swing.JInternalFrame implements JGSObserver {

	protected MainFrame mainFrame;

	ArrayList<JGSPanel> panels;
	protected Integer observerID;

	/**
	 * This is an intermediate model for .
	 */
	protected GraphBean graph;

	/**
	 * This constructor should never be called. Just added to Beans compatibility.
	 */
	public GraphIF() {
		
		panels = new ArrayList<JGSPanel>();
		initComponents();

		//TODO this should be crated dinamically from a config file or something, remember that the idea is get them as plugins.
		addPanel(new GraphicalPanel("Graphical Panel"));
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveDialog = new javax.swing.JFileChooser();
        tabs = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        mFile = new javax.swing.JMenu();
        Save = new javax.swing.JMenuItem();
        Close = new javax.swing.JMenuItem();
        mSolve = new javax.swing.JMenu();
        solveSpanningTree = new javax.swing.JMenuItem();
        solveSPT = new javax.swing.JMenuItem();
        mActions = new javax.swing.JMenu();
        reshape = new javax.swing.JMenuItem();

        saveDialog.setCurrentDirectory(new java.io.File("."));
        saveDialog.setDialogTitle("Save Graph");
        saveDialog.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        saveDialog.setName("saveDialog"); // NOI18N

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("graph");
        setName("Form"); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        tabs.setName("tabs"); // NOI18N
        getContentPane().add(tabs);
        tabs.getAccessibleContext().setAccessibleName("tabs");

        menuBar.setName("menuBar"); // NOI18N

        mFile.setText("File");
        mFile.setName("mFile"); // NOI18N

        Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        Save.setText("Save");
        Save.setName("Save"); // NOI18N
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });
        mFile.add(Save);

        Close.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        Close.setText("Close");
        Close.setName("Close"); // NOI18N
        Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseActionPerformed(evt);
            }
        });
        mFile.add(Close);

        menuBar.add(mFile);

        mSolve.setText("Solve");
        mSolve.setName("mSolve"); // NOI18N

        solveSpanningTree.setText("Spanning Tree");
        solveSpanningTree.setName("solveSpanningTree"); // NOI18N
        solveSpanningTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                solveSpanningTreeActionPerformed(evt);
            }
        });
        mSolve.add(solveSpanningTree);

        solveSPT.setText("Shortest Path Tree");
        solveSPT.setName("solveSPT"); // NOI18N
        solveSPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                solveSPTActionPerformed(evt);
            }
        });
        mSolve.add(solveSPT);

        menuBar.add(mSolve);

        mActions.setText("Actions");
        mActions.setName("mActions"); // NOI18N

        reshape.setText("Reshape");
        reshape.setName("reshape"); // NOI18N
        reshape.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reshapeActionPerformed(evt);
            }
        });
        mActions.add(reshape);

        menuBar.add(mActions);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseActionPerformed
		
		close();

	}//GEN-LAST:event_CloseActionPerformed

	private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
		
		close();

	}//GEN-LAST:event_formInternalFrameClosing

	private void solveSpanningTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_solveSpanningTreeActionPerformed
		
		//TODO add code to solve

}//GEN-LAST:event_solveSpanningTreeActionPerformed

    private void solveSPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_solveSPTActionPerformed
		
		//TODO add code to solve

    }//GEN-LAST:event_solveSPTActionPerformed

	private void reshapeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reshapeActionPerformed
		
		// TODO add code to reshape

	}//GEN-LAST:event_reshapeActionPerformed

	private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed

		//TODO add code to save files

	}//GEN-LAST:event_SaveActionPerformed

	//<editor-fold defaultstate="collapsed" desc="Autocreated members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JMenuItem Close;
    protected javax.swing.JMenuItem Save;
    protected javax.swing.JMenu mActions;
    protected javax.swing.JMenu mFile;
    protected javax.swing.JMenu mSolve;
    protected javax.swing.JMenuBar menuBar;
    protected javax.swing.JMenuItem reshape;
    protected javax.swing.JFileChooser saveDialog;
    protected javax.swing.JMenuItem solveSPT;
    protected javax.swing.JMenuItem solveSpanningTree;
    protected javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables
	//</editor-fold>

	/**
	 * This member is the controller used to manage the graph ruled by this internal frame.
	 */
	protected Controller controller;

	/**
	 * Returns the instance of the controller ruled by the internal frame.
	 * @return The Controller instance.
	 */
	public Controller getController() {
		return controller;
	}

	/**
	 * Sets te controller for the internal frame to make it able to manage the graph.
	 * @param controller
	 */
	public void setController(Controller controller) {
		this.controller = controller;
	}

	/**
	 * Closes properly the internal frame.
	 */
	private void close() {
		controller.closeGraph(graph.getID());
		this.dispose();
	}

	/**
	 * Adds a new panel to the frame.
	 * @param panel A <code>JGSPanel</code> that adds a new type of view of the graph.
	 */
	private void addPanel(JGSPanel panel){

		tabs.add(panel.getTitle(), panel);

	}
	
	//<editor-fold defaultstate="collapsed" desc="Observer Cascade Recalls">
	//////////////////////////////
	// OBSERVER CASCADE RECALLS //
	//////////////////////////////

	public Integer getId() {
		return observerID;
	}

	public void setId(Integer id) {
		this.observerID = id;
	}

	public void loadGraph(GraphBean graph) {
		this.graph = graph;
		for (JGSPanel panel : panels) {
			panel.loadGraph(graph);
		}
	}

	public void insertNode(NodeBean node) {
		this.graph.insertNode(node);
		for (JGSPanel panel : panels) {
			panel.insertNode(node);
		}
	}

	public void deleteNode(NodeBean node) {
		this.graph.deleteNode(node.getId());
		for (JGSPanel panel : panels) {
			panel.deleteNode(node);
		}
	}

	public void updateNode(NodeBean node) {
		this.graph.updateNode(node);
		for (JGSPanel panel : panels) {
			panel.updateNode(node);
		}
	}

	public void setSource(NodeBean node) {
		//TODO add the code here
	}

	public void unsetSource(NodeBean node) {
		//TODO add the code here
	}

	public void setSink(NodeBean node) {
		//TODO add the code here
	}

	public void unsetSink(NodeBean node) {
		//TODO add the code here
	}

	public void insertArc(ArcBean arc) {
		//TODO add the code here
	}

	public void deleteArc(ArcBean arc) {
		//TODO add the code here
	}

	public void updateArc(ArcBean arc) {
		//TODO add the code here
	}

	public void addDefaultNodeAttribute(Attribute attribute) {
		//TODO add the code here
	}

	public void removeDefaultNodeAttribute(Attribute attribute) {
		//TODO add the code here
	}

	public void updateDefaultNodeAttribute(String previousName, Attribute newValue) {
		//TODO add the code here
	}

	public void addDefaultArcAttribute(Attribute attribute) {
		//TODO add the code here
	}

	public void removeDefaultArcAttribute(Attribute attribute) {
		//TODO add the code here
	}

	public void updateDefaultArcAttribute(String previousName, Attribute newValue) {
		//TODO add the code here
	}
	//</editor-fold>
}
